package com.jzo2o.customer.controller.consumer;

import cn.hutool.core.bean.BeanUtil;
import com.jzo2o.api.customer.dto.response.AddressBookResDTO;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.customer.model.domain.AddressBook;
import com.jzo2o.customer.model.dto.request.AddressBookPageQueryReqDTO;
import com.jzo2o.customer.model.dto.request.AddressBookUpsertReqDTO;
import com.jzo2o.customer.service.IAddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 普通用户地址簿相关接口
 *"/customer/consumer/address-book/defaultAddress"
 * @author itcast
 * @create 2023/7/7 19:34
 **/
@RestController("consumerAddressBookController")
@RequestMapping("/consumer/address-book")
@Api(tags = "用户端 - 普通用户地址簿相关接口")
public class AddressBookController {

    @Autowired
    private IAddressBookService addressBookService;

    @GetMapping("/defaultAddress")
    @ApiOperation("查询用户默认地址")
    public AddressBookResDTO defaultAddress() {
        return addressBookService.defaultAddress();
    }


    //POST /customer/consumer/address-book
    @PostMapping()
    @ApiOperation("新增地址")
    public void addAddress(@RequestBody AddressBookUpsertReqDTO dto) {
        addressBookService.addAddress(dto);
    }

    @GetMapping("/page")
    @ApiOperation("查询地址簿列表")
    public PageResult<AddressBookResDTO> page(AddressBookPageQueryReqDTO dto) {
        return addressBookService.getPage(dto);
    }


    @GetMapping("/{id}")
    @ApiOperation("查询地址簿详情")
    public AddressBookResDTO getById(@PathVariable Long id) {
        AddressBook addressBook = addressBookService.getById(id);
        return BeanUtil.toBean(addressBook, AddressBookResDTO.class);
    }

    //接口路径：PUT  /customer/consumer/address-book/{id}
    @PutMapping("/{id}")
    @ApiOperation("地址簿编辑")
    public void update(@PathVariable Long id, @RequestBody AddressBookUpsertReqDTO dto) {
       addressBookService.updateAddressById(id, dto);
    }

    //接口路径：DELETE   /customer/consumer/address-book/batch
    @DeleteMapping("/batch")
    @ApiOperation("批量删除地址簿")
    public void delete(@RequestBody List<Long> ids) {
        addressBookService.removeByIds(ids);
    }

    //接口路径：PUT  /customer/consumer/address-book/default
    @PutMapping("/default")
    @ApiOperation("设置默认地址")
    public void setDefault(Integer flag,Long id) {
        addressBookService.updateDefault(flag,id);
    }
}
